package com.sjm.suchbear.minibookmall;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.sjm.suchbear.minibookmall.fragment.BookFragment;
import com.sjm.suchbear.minibookmall.fragment.CarFragment;
import com.sjm.suchbear.minibookmall.fragment.MyFragment;
import com.sjm.suchbear.minibookmall.fragment.NoticeFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends Activity {

    @BindView(R.id.fgment)
    FrameLayout fgment;
    @BindView(R.id.tv_book)
    TextView tvBook;
    @BindView(R.id.tv_car)
    TextView tvCar;
    @BindView(R.id.tv_my)
    TextView tvMy;
    @BindView(R.id.tv_notice)
    TextView tvNotice;

    Fragment carFragment;
    Fragment bookFragment;
    Fragment myFragment;
    Fragment noticeFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        if (savedInstanceState != null) {
            Fragment tagfragment = fragmentManager.findFragmentByTag("tag");
            fragmentTransaction.add(R.id.fgment, tagfragment, "tag");
        } else {
            bookFragment = new BookFragment();
            fragmentTransaction.add(R.id.fgment, bookFragment, "tag");
        }
        fragmentTransaction.commit();
    }


    @OnClick({R.id.tv_book, R.id.tv_car, R.id.tv_my, R.id.tv_notice})
    public void onViewClicked(View view) {
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        switch (view.getId()) {
            case R.id.tv_book:
                bookFragment = new BookFragment();
                fragmentTransaction.replace(R.id.fgment, bookFragment, "tag");
                fragmentTransaction.commit();
                break;
            case R.id.tv_car:
                carFragment = new CarFragment();
                fragmentTransaction.replace(R.id.fgment, carFragment, "tag");
                fragmentTransaction.commit();
                break;
            case R.id.tv_my:
                myFragment = new MyFragment();
                fragmentTransaction.replace(R.id.fgment, myFragment, "tag");
                fragmentTransaction.commit();
                break;
            case R.id.tv_notice:
                noticeFragment = new NoticeFragment();
                fragmentTransaction.replace(R.id.fgment, noticeFragment, "tag");
                fragmentTransaction.commit();
                break;
        }
    }
}
